﻿using System.ComponentModel.Composition;
using System.Data;
using System.Linq;
using System.Web.Mvc;

using iTravel.Data;
using iTravel.Repository;

namespace iTravel.Web.Controllers
{
    public class FlightController : Controller
    {
        private readonly IFlightRepository _flightRepository;

        public FlightController()
        {            
        }

        public FlightController([Import]IFlightRepository flightRepository)
        {
            _flightRepository = flightRepository;
        }

        public ActionResult Index()
        {
                return View(_flightRepository.All());
        }

        public ActionResult Details(int id)
        {
            using (TravelContext context = new TravelContext())
            {
                return View(context.Flights.Single(f => f.Id == id));
            }
        }

        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(Flight flight)
        {

            using (TravelContext context = new TravelContext())
            {
                context.Flights.Add(flight);
                context.SaveChanges();
            }

            return RedirectToAction("Index");

        }

        public ActionResult Edit(int id)
        {
            using (TravelContext context = new TravelContext())
            {
                return View(context.Flights.Single(f => f.Id == id));
            }
        }

        [HttpPost]
        public ActionResult Edit(int id, Flight flight)
        {

            using (TravelContext context = new TravelContext())
            {
                context.Entry(flight).State = EntityState.Modified;
                context.SaveChanges();
            }
            return RedirectToAction("Index");

        }

        public ActionResult Delete(int id)
        {
            using (TravelContext context = new TravelContext())
            {
                return View(context.Flights.Single(f => f.Id == id));
            }
        }

        [HttpPost]
        public ActionResult Delete(int id, Flight flight)
        {

            using (TravelContext context = new TravelContext())
            {
                context.Entry(flight).State = EntityState.Deleted;
                context.SaveChanges();
            }

            return RedirectToAction("Index");
        }
    }
}